package com.shiguiwu.springboot3.java9;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @description: java9的新特性
 * @author: stone
 * @date: Created by 2023/7/9 16:25
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.java9
 */
@Slf4j
public class Java9Tests {


    /**
     * 接口私有方法
     */
    @Test
    public void testApi() {
        Java9Api java9Api = new Java9Api() {
            @Override
            public void test() {
                log.info("==========================");
                Java9Api.super.test();
            }
        };
        java9Api.test();
    }

    /**
     * 集合工厂,返回的是不可变集合
     */
    @Test
    public void testCollection() {
        List<Object> objects = List.of(1, 2, new Object());
        List<? extends Number> numbers = List.of(2, 3, 4, 1.2);

        Set<String> a = Set.of("a", "b");
        Map<String, Integer> a1 = Map.of("a", 1, "b", 2);
        log.info("集合工厂 ===> ");
    }

    /**
     * 变量流
     */
    @Test
    public void testStream() {
        Stream.of(1, 23, 4, 6, 7)
                //从第一个开始判断，知道不满足退出
                .takeWhile(x -> x < 6)
                .forEach(x -> log.info("输出对象 ==> {}", x));

        Stream.of(4, 4, 5, 6, 8)
                //从第一个不满足开始，到最后
                .dropWhile(x -> x < 5)
                .forEach(x -> log.info("输出对象1 ==> {}", x));

        //如果是空，啥也不输出
        Stream.ofNullable("null")
                .forEach(x -> log.info("输出对象2 ==> {}", x));

        //遍历
        Stream.iterate(1,x -> x < 3,y -> y + 2)
                .forEach(x -> log.info("输出对象3 ==> {}", x));
    }
}
